package com.ruoyi.project.system.photos.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.project.system.album.domain.BizAlbum;
import com.ruoyi.project.system.album.service.IBizAlbumService;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.photos.domain.BizPhotos;
import com.ruoyi.project.system.photos.service.IBizPhotosService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 图片Controller
 * 
 * @author ruoyi
 * @date 2020-05-01
 */
@Controller
@RequestMapping("/system/photos")
public class BizPhotosController extends BaseController
{
    private String prefix = "system/photos";

    @Autowired
    private IBizPhotosService bizPhotosService;

    @Autowired
    private IBizAlbumService bizAlbumService;

    @RequiresPermissions("system:photos:view")
    @GetMapping()
    public String photos(ModelMap mmap)
    {
        List<BizAlbum> albums = this.bizAlbumService.selectBizAlbumList(null);
        mmap.put("albums", albums);
        return prefix + "/photos";
    }

    /**
     * 查询图片列表
     */
    @RequiresPermissions("system:photos:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizPhotos bizPhotos)
    {
        List<BizAlbum> albums = this.bizAlbumService.selectBizAlbumList(null);
        Map<Integer, String> albumsMap = new HashMap<>();
        albums.forEach(album -> {
            albumsMap.put(album.getId(), album.getTitle());
        });
        startPage();
        List<BizPhotos> list = bizPhotosService.selectBizPhotosList(bizPhotos);
        list.forEach(photo -> {
            photo.setAlbumTitle(albumsMap.get(photo.getAlbumId()));
        });
        return getDataTable(list);
    }

    /**
     * 导出图片列表
     */
    @RequiresPermissions("system:photos:export")
    @Log(title = "图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizPhotos bizPhotos)
    {
        List<BizPhotos> list = bizPhotosService.selectBizPhotosList(bizPhotos);
        ExcelUtil<BizPhotos> util = new ExcelUtil<BizPhotos>(BizPhotos.class);
        return util.exportExcel(list, "photos");
    }

    /**
     * 新增图片
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        // 返回相册列表
        List<BizAlbum> albums = this.bizAlbumService.selectBizAlbumList(null);
        mmap.put("albums", albums);
        return prefix + "/add";
    }

    /**
     * 新增保存图片
     */
    @RequiresPermissions("system:photos:add")
    @Log(title = "图片", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizPhotos bizPhotos)
    {
        return toAjax(bizPhotosService.insertBizPhotos(bizPhotos));
    }

    /**
     * 修改图片
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        List<BizAlbum> albums = this.bizAlbumService.selectBizAlbumList(null);
        mmap.put("albums", albums);
        BizPhotos bizPhotos = bizPhotosService.selectBizPhotosById(id);
        mmap.put("bizPhotos", bizPhotos);
        return prefix + "/edit";
    }

    /**
     * 修改保存图片
     */
    @RequiresPermissions("system:photos:edit")
    @Log(title = "图片", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizPhotos bizPhotos)
    {
        return toAjax(bizPhotosService.updateBizPhotos(bizPhotos));
    }

    /**
     * 删除图片
     */
    @RequiresPermissions("system:photos:remove")
    @Log(title = "图片", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizPhotosService.deleteBizPhotosByIds(ids));
    }
}
